﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media.Imaging;

namespace MediaBrowser.Gui.Converters
{
    /// <summary>
    /// Cette classe convertit une valeur String en un un Bitmap. 
    /// </summary>
    [ValueConversion(typeof(String), typeof(BitmapImage))]
    public class StringToDragBitmapConverter : MarkupExtension, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            String urlImg = value as String;
            try
            {
                BitmapImage img = new BitmapImage();
                if (urlImg != null)
                {
                    img.BeginInit();
                    img.CacheOption = BitmapCacheOption.OnLoad;
                    img.UriSource = new Uri(urlImg);
                    img.EndInit();
                }
                else
                {
                    img.BeginInit();
                    img.CacheOption = BitmapCacheOption.OnLoad;
                    img.UriSource = new Uri("pack://application:,,,/Images/DragDrop.png");
                    img.EndInit();
                }
                return img;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return new StringToDragBitmapConverter();
        }

    }
}
